<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// 模拟家具数据
const furnitureItems = ref([
  {
    id: 1,
    name: '舒适布艺沙发',
    price: 3999,
    image: 'https://picsum.photos/id/1067/300/200',
    category: '沙发',
    description: '温暖的布艺沙发，柔软舒适，适合家庭休闲时光。'
  },
  {
    id: 2,
    name: '木质单人床架',
    price: 5999,
    image: 'https://picsum.photos/id/1060/300/200',
    category: '床',
    description: '简约木质床架，温暖自然，营造舒适睡眠环境。'
  },
  {
    id: 3,
    name: '橡木餐桌套装',
    price: 7999,
    image: 'https://picsum.photos/id/174/300/200',
    category: '餐桌',
    description: '实木餐桌套装，温馨自然，适合家庭用餐。'
  },
  {
    id: 4,
    name: '休闲靠背椅',
    price: 1999,
    image: 'https://picsum.photos/id/1029/300/200',
    category: '椅子',
    description: '舒适的休闲椅，温暖色调，适合阅读和放松。'
  }])

// 获取路由和路由实例
const route = useRoute()
const router = useRouter()

// 当前选中的分类
const currentCategory = ref(route.query.category || '全部')

// 过滤后的家具列表
const filteredFurniture = computed(() => {
  if (currentCategory.value === '全部') {
    return furnitureItems.value
  }
  return furnitureItems.value.filter(item => item.category === currentCategory.value)
})

// 获取所有分类
const categories = computed(() => {
  const validCategories = ['沙发', '床', '餐桌', '椅子'];// 已移除衣柜分类
  return ['全部', ...validCategories];
})

// 切换分类
const changeCategory = (category) => {
  currentCategory.value = category
  router.push({ query: { category } })
}

// 查看详情
const viewDetail = (id) => {
  router.push({ name: 'detail', params: { id } })
}
</script>

<template>
  <div class="furniture-container">
    <h2>家具展示</h2>

    <!-- 分类筛选 -->
    <div class="categories">
      <button
        v-for="category in categories"
        :key="category"
        @click="changeCategory(category)"
        :class="{ active: currentCategory === category }"
      >
        {{ category }}
      </button>
    </div>

    <!-- 家具列表 -->
    <div class="furniture-list">
      <div class="furniture-item" v-for="item in filteredFurniture" :key="item.id">
        <img :src="item.image" alt="{{ item.name }}" class="furniture-image" @click="viewDetail(item.id)">
        <div class="furniture-info">
          <h3>{{ item.name }}</h3>
          <p class="category">{{ item.category }}</p>
          <p class="price">¥{{ item.price }}</p>
          <p class="description">{{ item.description }}</p>
          <button @click="viewDetail(item.id)" class="detail-btn">查看详情</button>
          <button @click="$emit('bookNow', item.id)" class="book-btn">立即预约</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.furniture-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

h2 {
  text-align: center;
  color: #5D4037;
  margin-bottom: 20px;
}

.categories {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
  justify-content: center;
}

.categories button {
  padding: 12px 20px;
font-size: 14px;
  background-color: #FFF3E0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.categories button.active {
  background-color: #FFB347;
  color: white;
}

.furniture-list {
  column-count: 1;
  padding: 0 15px;
}

@media (min-width: 768px) {
  .furniture-list {
    column-count: 3;
    padding: 0;
  }
}

.furniture-item {
  break-inside: avoid;
  margin-bottom: 15px;
  border-radius: 12px;
}

.furniture-image {
  height: 250px;
}

.furniture-item {
  border: 1px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
}

.furniture-item:hover {
  transform: translateY(-5px);
}

.furniture-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.furniture-info {
  padding: 16px;
}

.furniture-info h3 {
  margin-top: 0;
  color: #333;
}

.category {
  color: #666;
  font-size: 14px;
  margin: 5px 0;
}

.price {
  color: #e74c3c;
  font-weight: bold;
  font-size: 18px;
  margin: 5px 0;
}

.description {
  color: #666;
  font-size: 14px;
  margin-bottom: 15px;
}

.detail-btn,
.book-btn {
  padding: 8px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  margin-right: 10px;
}

.detail-btn {
  background-color: #f0f0f0;
  color: #333;
}

.book-btn {
  background-color: #42b883;
  color: white;
}
</style>